package com.itluma.isWOak.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.mapper.AdminInfoMapper;
import com.itluma.isWOak.pojo.AdminInfo;
import com.itluma.isWOak.service.AdminInfoService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {
    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Override
    public PageInfo<AdminInfo> page(Integer pageNum, Integer limit, Integer moduleId, String roleName) {
        PageHelper.startPage(pageNum, limit);
        List<AdminInfo> list = adminInfoMapper.list(moduleId, roleName);
        PageInfo<AdminInfo> pageList = new PageInfo<>(list);
        return pageList;
    }

    @Override
    public AdminInfo findById(Integer id) {
        AdminInfo adminInfo = adminInfoMapper.findByAdminId(id);
        return adminInfo;
    }

    @Override
    public boolean resetPassword(Integer... ids) {
        boolean flag = true;
        //密码重置为“123456”
        String password = "123456";
        for (int i = 0; i < ids.length; i++) {
            String resPassword = new Md5Hash(password, adminInfoMapper.findByAdminId(ids[i]).getAdminCode(), 10).toString();
            if(adminInfoMapper.resetPassword(ids[i], resPassword) <= 0){
                flag = false;
            }
        }
        return flag;
    }

    @Override
    public void modify(AdminInfo adminInfo) {
        adminInfoMapper.update(adminInfo);
    }

    @Override
    public void add(AdminInfo adminInfo) {
        adminInfo.setPassword(new Md5Hash(adminInfo.getPassword(), adminInfo.getAdminCode(), 10).toString());
        adminInfoMapper.insert(adminInfo);
    }

    @Override
    public void deleteByAdminId(Integer adminId) {
        adminInfoMapper.deleteByAdminId(adminId);
    }

    @Override
    public AdminInfo getByAdminCode(String adminCode) {

        return adminInfoMapper.getByAdminCode(adminCode);
    }

}
